﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Data.SqlClient;
using Atlandiz.Data;
using System.Runtime.InteropServices;

namespace Atlandiz.EnvanterVeZimmetYonetimi
{
    public partial class FormNesne : DevExpress.XtraEditors.XtraForm
    {
        MsSqlAccessLayer SQL = new MsSqlAccessLayer(Army.SQL().ConnStr);
        public int Urun_Id = 0;
        public int UrunTipi = 0;
        public int KayId = 0;

        public FormNesne()
        {
            InitializeComponent();
        }

        private void FormYeniNesne_Load(object sender, EventArgs e)
        {
            isHurdaTasfiye.Checked = false;
            if (KayId>0) {
                //MessageBox.Show(SQL.GetDataCell("Select DosyaAdi from Nesneler where id=" + KayId));
                _txtFileUrl.Text = "Önceden Kaydedilen Nesne. " + SQL.GetDataCell("Select DosyaAdi from Nesneler where id=" + KayId);
                _txtAciklama.Text = SQL.GetDataCell("Select Aciklama from Nesneler where id=" + KayId);
                isHurdaTasfiye.Checked=Convert.ToBoolean(SQL.GetDataCell("Select isHurdaTasfiyeTutanak from Nesneler where id=" + KayId));
            }
        }

        private void _btnKapat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void DosyaSec()
        {
            openFileDialog1.Title = "Lütfen Dosya Seçiniz";
            //openFileDialog1.Filter = " (*.jpg)|*.jpg|(*.png)|*.png";
            //openFileDialog1.FilterIndex = 1;
            string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFileDialog1.InitialDirectory = filePath;

            openFileDialog1.ShowDialog();
            _txtFileName.Text = System.IO.Path.GetFileName(openFileDialog1.FileName);
            _txtFileUrl.Text = openFileDialog1.FileName;

            //openFileDialog1.Multiselect = true;
            //formultiselect please non-comment lines from this..
            //foreach (string str in openFileDialog1.FileNames)
            //{
            //    //_txtFileUrl.Text += str + ";\n";
            //    _txtFileUrl.Text = str; 
            //}
              

        }

        private void _btnDepartmanListesi_Click(object sender, EventArgs e)
        {

        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            DosyaSec();
        }

        private void _btnKaydet_Click(object sender, EventArgs e)
        {

            if (KayId > 0)
            {

                string UptSrg = "Update Nesneler set Aciklama='" + _txtAciklama.Text.Trim().ToString() + "',isHurdaTasfiyeTutanak=" + Convert.ToInt32(isHurdaTasfiye.Checked) + " where id=" + KayId;
                SQL.cmd(UptSrg);
                MessageBox.Show("Açıklama Güncellendi.");
                this.Close();

            }
            else
            {
                if (_txtFileUrl.Text.Length > 0)
                {

                    SqlConnection conn = new SqlConnection(SQL.ConnStr);


                    //FileStream st = new FileStream(_txtFileUrl.Text, FileMode.Open);
                    //byte[] buffer = new byte[st.Length];
                    //st.Read(buffer, 0, (int)st.Length);
                    //st.Close();

                    byte[] fileContent = File.ReadAllBytes(_txtFileUrl.Text);

                    SqlCommand FileEkleKomut = new SqlCommand(
                             "INSERT INTO Nesneler " +
                             "(Urun_ID,UrunTipi,DosyaAdi,Aciklama,Nesne,isHurdaTasfiyeTutanak,isDeleted) " +
                             "VALUES(" + Urun_Id + "," + UrunTipi + ",@DosyaAdi,'" + _txtAciklama.Text.Trim().ToString() + "',@Dosya," + Convert.ToInt32(isHurdaTasfiye.Checked) + ",0)", conn);

                    FileEkleKomut.Parameters.Add("@DosyaAdi", SqlDbType.NVarChar, 1000).Value = _txtFileName.Text.Replace(" ", "");
                    FileEkleKomut.Parameters.Add("@Dosya", SqlDbType.VarBinary, int.MaxValue).Value = fileContent;

                    conn.Open();
                    FileEkleKomut.ExecuteScalar();
                    conn.Close();


                    MessageBox.Show("Dosya Kaydedildi.");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Dosya Seçmediniz.");
                }
            }
            
        }

        public static bool HasExecutable(string path)
        {
            var executable = FindExecutable(path);
            return !string.IsNullOrEmpty(executable);
        }

        private static string FindExecutable(string path)
        {
            var executable = new StringBuilder(1024);
            FindExecutable(path, string.Empty, executable);
            return executable.ToString();
        }

        [DllImport("shell32.dll", EntryPoint = "FindExecutable")]
        private static extern long FindExecutable(string lpFile, string lpDirectory, StringBuilder lpResult);

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                if (_txtFileUrl.Text.Length > 0)
                {

                    if (HasExecutable(_txtFileUrl.Text.ToString()))
                    {

                        //MessageBox.Show(FindExecutable(_txtFileUrl.Text.ToString()));
                        System.Diagnostics.Process.Start(_txtFileUrl.Text.ToString());
                    }

                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }


        }

    }
}